package ru.hh.team02.chat.client;

import ru.hh.team02.chat.messaging.SendingQueue;
import ru.hh.team02.chat.server.ChatServer;

/**
 * Created with IntelliJ IDEA.
 * User: yan4ik
 * Date: 12/23/12
 * Time: 6:19 AM
 * To change this template use File | Settings | File Templates.
 */
public class MessageHandler {
    private String clientName;

    public MessageHandler(String clientName) {
        this.clientName = clientName;
    }

    public void markMessageInDb(String message) {
        int messageId = Integer.parseInt(message.split(" ")[1]);
        ChatServer.dbService.markPersonalMessageAsDelivered(messageId);
    }

    public void changeRoom(String message) {
        String[] info = message.split(" ");
        if (info.length == 2) {
            String roomName = info[1];

            ChatServer.REGISTRY.changeRoom(clientName, roomName);
            ChatServer.ADMIN_LOGGER.write("Client \"" + clientName
                    + "\" entered in new room \"" + roomName + "\"");
        }
    }

    public void sendPersonalMessage(String message) {
        String[] info = message.split(" ", 3);
        if (info.length == 3) {
            String recepientName = info[1];
            String messageText = info[2];

            ChatServer.MESSENGER.trySendPersonalMessage("personal:" + clientName + "> " +
                    messageText,
                    recepientName);
            ChatServer.ADMIN_LOGGER.write("Client \"" + clientName
                    + "\" sent new message: \""
                    + message + "\"");
        }
    }

    public void sendMessageToRoom(String message) {
        ChatServer.MESSENGER.sendMessageToRoom(clientName + "> " + message,
                clientName);
        ChatServer.ADMIN_LOGGER.write("Client \"" + clientName
                + "\" sent new message: \"" + message + "\"");
    }

}
